package wang.aweb.childcar.controller.management;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.common.Constant;
import wang.aweb.childcar.exception.CarException;
import wang.aweb.childcar.exception.CarExceptionEnum;
import wang.aweb.childcar.model.pojo.Administrator;
import wang.aweb.childcar.model.pojo.Member;
import wang.aweb.childcar.model.req.LoginReq;
import wang.aweb.childcar.service.AdministratorService;

import java.io.UnsupportedEncodingException;
import java.util.Date;

@RestController
@RequestMapping("/management/")
public class LoginController {
    @Autowired
    private AdministratorService administratorService;
    @GetMapping("administrator/info")
    public ApiRestResponse info(@RequestParam String token) throws UnsupportedEncodingException {
        Algorithm algorithm=Algorithm.HMAC256(Constant.JWT_KEY);
        JWTVerifier jwtVerifier= JWT.require(algorithm).build();
        Administrator administrator=new Administrator();
        try{
            DecodedJWT jwt=jwtVerifier.verify(token);
            administrator=administratorService.findById(jwt.getClaim(Constant.USER_ID).asInt());
        }catch (TokenExpiredException e){
            throw new CarException(CarExceptionEnum.TOKEN_EXPIRED);
        }catch (JWTDecodeException e){
            throw new CarException(CarExceptionEnum.TOKEN_WRONG);
        }catch (Exception e){
            throw new CarException(CarExceptionEnum.SYSTEM_ERROR);
        }
        return ApiRestResponse.success(administrator);
    }
    @ApiOperation("登录接口")
    @PostMapping("administrator/login")
    @ResponseBody
    public ApiRestResponse login(@RequestBody LoginReq loginReq) throws UnsupportedEncodingException {
        if(StringUtils.isEmpty(loginReq.getUsername())){
            return ApiRestResponse.error(CarExceptionEnum.NEED_USER_NAME);
        }
        if(StringUtils.isEmpty(loginReq.getPassword())){
            return ApiRestResponse.error(CarExceptionEnum.NEED_PASSWORD);
        }
        Administrator user=administratorService.doLogin(loginReq);
        Algorithm algorithm= Algorithm.HMAC256(Constant.JWT_KEY);
        String token= JWT.create().withClaim(Constant.USER_NAME,user.getUsername())
                .withClaim(Constant.USER_ID,user.getId())
                .withClaim(Constant.USER_ROLE,3)
                .withExpiresAt(new Date(System.currentTimeMillis()+Constant.EXPIRE_TIME))
                .sign(algorithm);
        user.setPassword(null);
        user.setToken(token);
        return ApiRestResponse.success(user);
    }
}
